#include "StdAfx.h"
#include "RGB2HSV.h"


vector<int> RGB2HSV::ApplyFilter(int red, int green, int blue)
{
	vector<int> Results(3,0);
	int h = 0, s = 0, v = 0;

	double maxHSV = (double)(MAX(red, MAX(green, blue)));
	double minHSV = (double)(MIN(red, MIN(green, blue)));

	v = (int)(maxHSV);

	if (maxHSV != 0)
	{
		s = (int)(255 - 255 * (minHSV / maxHSV));
	}

	if (maxHSV != minHSV)
	{
		int IntMaxHSV = (int)(maxHSV);

		if (IntMaxHSV == red && green >= blue)
		{
			h = (int)(60 * (green - blue) / (maxHSV - minHSV));
		} 
		else if (IntMaxHSV == red && green < blue)
		{
			h = (int)(359 + 60 * (green - blue) / (maxHSV - minHSV));
		}
		else if (IntMaxHSV == green)
		{
			h = (int)(119 + 60 * (blue - red) / (maxHSV - minHSV));
		}
		else if (IntMaxHSV == blue)
		{
			h = (int)(239 + 60 * (red - green) / (maxHSV - minHSV));
		}
	}
	else
	{
		h = 0;
	}

	Results[0] = h;
	Results[1] = s;
	Results[2] = v;

	return (Results);
}

